using LibGit2Sharp;
using System.Text;

namespace MultiAgentsServer.Scripts.Services;

public class FileOperationService
{
    public static bool CheckFileExist(string filePath)
    {
        // Get the directory part of the file path
        //Process.Start("explorer.exe", $"/select,\"{filePath}\"");

        return File.Exists(filePath);
    }

    public static string LoadFile(string filePath)
    {
        try
        {
            // Get the directory part of the file path
            //Process.Start("code", $"--goto \"{filePath}\"");

            return File.ReadAllText(filePath);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    public static string CreateFile(string filePath, string fileContent)
    {
        try
        {
            // Get the directory path from the file path
            string? directoryPath = Path.GetDirectoryName(filePath);

            // Check if the directory path is not null or empty
            if (!string.IsNullOrEmpty(directoryPath))
            {
                // Create the directory if it does not exist
                Directory.CreateDirectory(directoryPath);
            }

            // Write the content to the file, this will automatically create the file
            File.WriteAllText(filePath, fileContent);

            return "File Creation Success";
        }
        catch (Exception ex)
        {
            // Return the exception message if an error occurs
            return ex.Message;
        }
    }

    public static string ExploreDirectory(string rootDirectory, int depth = -1, int currentLevel = 0)
    {
        List<string> ignoreDirCollection =
        [
            ".vs",
            "packages",
            ".git",
            ".idea",
            "vcpkg_installed",
            "x64"
        ];

        if (!Directory.Exists(rootDirectory) || depth == 0 || ignoreDirCollection.Contains(Path.GetFileName(rootDirectory)))
        {
            return string.Empty;
        }

        StringBuilder sb = new();
        string indent = new(' ', currentLevel * 4); // 4 spaces for each level of depth

        // Add the directory name as a header based on its depth level
        sb.AppendLine($"{indent}- **{new DirectoryInfo(rootDirectory).Name}**");

        // Using LibGit2Sharp to check for ignored files
        using (Repository repo = new(Repository.Discover(rootDirectory)))
        {
            // Explore files in the current directory
            foreach (var file in Directory.GetFiles(rootDirectory))
            {
                if (!repo.Ignore.IsPathIgnored(file))
                {
                    sb.AppendLine($"{indent}  - {Path.GetFileName(file)}");
                }
            }

            // Recursively explore subdirectories if not at the specified depth
            if (depth == -1 || currentLevel < depth - 1)
            {
                foreach (var directory in Directory.GetDirectories(rootDirectory))
                {
                    if (!repo.Ignore.IsPathIgnored(directory))
                    {
                        sb.Append(ExploreDirectory(directory, depth, currentLevel + 1));
                    }
                }
            }
        }

        return sb.ToString();
    }
}
